package com.uroad.jilinpda.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.uroad.jilinpda.common.GlobalData;
import com.uroad.jilinpda.webservice.BaseWS;
import com.uroad.lib.data.Constant;
import com.uroad.lib.util.log.LogUtil;

/**
 * 创建者 : wlw
 * 创建时间 : 2020/6/20 9:51
 * 描述 : TODO 网络工具类
 */
public class NetWorkUtil {
    private static Context mContext;
    /**
     * 获取对当前网络类型
     * @param context
     * @return
     */
    public static String getCurrNetWork(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isAvailable()) {
            switch (networkInfo.getType()) {
                case ConnectivityManager.TYPE_MOBILE://2G 3G 4G 网络
                    return "4G";
                case ConnectivityManager.TYPE_WIFI://WIFI网络
                    return "WIFI";
            }
        } else {//无网络
            return "";
        }
        return "";
    }
    @SuppressLint("HandlerLeak")
    private static Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    Constant.NET = "4G";
                    if (!"4G".equalsIgnoreCase(Constant.NET)) {
                        if (mContext != null) {
                            ToastUtil.show(mContext,"当前为内部网络");
                        }
                    }
                    break;
                case 2:
                    Constant.NET = "WIFI";
                    if (!"WIFI".equalsIgnoreCase(Constant.NET)) {
                        ToastUtil.show(mContext,"当前为内部网络");
                    }
                    break;
            }
        }
    };

    public static void checkNetwork(Context context){
        mContext = context;
        PriorityExecutor.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                judgeTheConnect(BaseWS.pingIp);
            }
        });
    }

    /**
     * =========通过ip ping 来判断ip是否通
     *
     * @param ip
     */
    private static void judgeTheConnect(String ip) {
        try {
            if (!TextUtils.isEmpty(ip)) {
                LogUtil.i("wlw","ping ip开始");
                //代表ping 3 次 超时时间为10秒
                Process p = Runtime.getRuntime().exec("ping -c 3 -w 10 " + ip);//ping3次
                int status = p.waitFor();
                LogUtil.i("wlw", "ping ip结束：" + status);
                if (!GlobalData.isCheckNetwork) {
                    return;
                }
                if (status == 0) {
                    //代表成功
                    Message message = new Message();
                    message.what = 1;
                    handler.sendMessage(message);
                } else {
                    //代表失败
                    Message message = new Message();
                    message.what = 2;
                    handler.sendMessage(message);
                }
            }
        } catch (Exception e) {
            //代表异常
            e.printStackTrace();
        }

    }
}
